package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.enums.OrderEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.equipment.entity.*;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepTaskPickupLineService;
import com.mxpio.erp.common.jpa.model.OrderLine;
import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.order.service.OrderLineService;
import com.mxpio.erp.common.vo.NoticeClassVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Tag(name = "EquipmentUpkeepTaskPickupLineController", description = "设备保养-领料明细数据接口")
@RestController
@RequestMapping("/erp/equipment/upkeep/task/pickup/")
public class EquipmentUpkeepTaskPickupLineController {

    @Autowired
    private EquipmentUpkeepTaskPickupLineService service;

    @Autowired
    private OrderLineService orderLineService;

    @GetMapping("list")
    @Operation(summary = "设备保养-领料明细列表", description = "获取设备保养-领料明细列表（无分页）", method = "GET")
    public Result<List<EquipmentUpkeepTaskPickupLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentUpkeepTaskPickupLine> EquipmentUpkeepTaskPickupLine = service.list(EquipmentUpkeepTaskPickupLine.class, criteria);
        return Result.OK(EquipmentUpkeepTaskPickupLine);
    }

    @GetMapping("page")
    @Operation(summary = "设备保养-领料明细列表", description = "获取设备保养-领料明细列表（分页）", method = "GET")
    @Transactional(readOnly = true)
    public Result<Page<EquipmentUpkeepTaskPickupLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentUpkeepTaskPickupLine> EquipmentUpkeepTaskPickupLine = service.listPage(EquipmentUpkeepTaskPickupLine.class,page, criteria);
        return Result.OK(EquipmentUpkeepTaskPickupLine);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取设备保养-领料明细", description = "根据bizNo获取设备保养-领料明细", method = "GET")
    @Transactional(readOnly = true)
    public Result<List<EquipmentUpkeepTaskPickupLine>> getByBizNo(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<EquipmentUpkeepTaskPickupLine> pickupLines = service.list(EquipmentUpkeepTaskPickupLine.class, c);
        if (pickupLines.size()>0){
            NoticeClassVo pickNoticeClazz = new NoticeClassVo(UpkeepPickupNotice.class, UpkeepPickupNoticeLine.class);
            NoticeClassVo returnNoticeClazz = new NoticeClassVo(UpkeepReturnBomNotice.class, UpkeepReturnBomNoticeLine.class);
            List<NoticeClassVo> noticeClassVos = new ArrayList<>();
            noticeClassVos.add(pickNoticeClazz);
            noticeClassVos.add(returnNoticeClazz);
            orderLineService.handleBizOrderLineQuantity(pickupLines, noticeClassVos, OrderEnums.AccessType.OUT.getCode());
        }
        return Result.OK(pickupLines);
    }


    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除设备保养-领料明细", description = "删除设备保养-领料明细,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "DELETE")
    public Result<EquipmentUpkeepTaskPickupLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            service.remove(EquipmentUpkeepTaskPickupLine.class, lineKey);
        }
        return Result.OK();
    }

    @PutMapping("open/{ids}")
    @Operation(summary = "打开设备保养-领料明细", description = "打开设备保养-领料明细,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "PUT")
    public Result<? extends OrderLine> open(@PathVariable(name = "ids", required = true) String ids) {
        return orderLineService.updateCloseStatus(EquipmentUpkeepTaskPickupLine.class, ids, OrderStatusEnums.CloseStatus.OPEN);
    }

    @PutMapping("close/{ids}")
    @Operation(summary = "关闭设备保养-领料明细", description = "关闭设备保养-领料明细", method = "PUT")
    public Result<? extends OrderLine> close(@Parameter(description="业务单号，多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后") @PathVariable(name = "ids", required = true) String ids) {
        return orderLineService.updateCloseStatus(EquipmentUpkeepTaskPickupLine.class, ids, OrderStatusEnums.CloseStatus.CLOSED);
    }

}
